import json
import random
import time

import requests
from django.shortcuts import render

# Create your views here.
from django.views import View

from apps.payment.models import OrderInfo
from utils.singer_detail import vip


class Mv_Detail_View(View):
    """添加mv详情页"""
    def get(self, request):
        # 获取最新MV
        mv_ids = request.GET.get('mv_id')
        one_mv_url = 'http://musicapi.leanapp.cn/mv/detail?mvid=' + mv_ids
        content = requests.get(one_mv_url)
        ret = json.loads(content.text)

        mv_name = ret['data']['name']  # mv名字
        mv_id = ret['data']['id']  # mv_id
        mv_time = ret['data']['publishTime']  # mv时间
        singer_name = ret['data']['artistName']  # 歌手名字
        singer_id = ret['data']['artistId']  # 歌手id
        mv_240 = ret['data']['brs']['240']  # 清晰度
        mv_480 = ret['data']['brs']['480']  # 清晰度
        try:
            mv_720 = ret['data']['brs']['720']  # 清晰度
            mv_1080 = ret['data']['brs']['1080']  # 清晰度
        except Exception as e:
            mv_720 = mv_240
            mv_1080 = mv_480
        mv_desc = ret['data']['desc']  # mv介绍
        mv_cover = ret['data']['cover']  # mv图片
        mv_play = ret['data']['playCount']  # mv播放量


        # 获取mv评论
        comment_url = 'http://musicapi.leanapp.cn/comment/mv?id=' + mv_ids
        content = requests.get(comment_url)
        comment_ret = json.loads(content.text)
        comment_hot_list = []
        comment_list = []

        b = 0
        try:
            while b <= 20:
                user_id = comment_ret['hotComments'][b]['user']['userId']  # 用户id
                user_name = comment_ret['hotComments'][b]['user']['nickname']  # 用户名字
                user_pic = comment_ret['hotComments'][b]['user']['avatarUrl']  # 用户头像
                content = comment_ret['hotComments'][b]['content']  # 用户评论信息
                comment_time = comment_ret['hotComments'][b]['time']  # 评论时间
                user_like = comment_ret['hotComments'][b]['likedCount']  # 用户点赞量
                comment_id = comment_ret['hotComments'][b]['commentId']  # 评论id

                t = str(comment_time)
                p = t[0:10]
                format = '%Y-%m-%d'
                value = time.localtime(int(p))
                hot = time.strftime(format, value)
                b += 1

                comment_dict = {
                    'user_id': user_id,
                    'user_name': user_name,
                    'user_pic': user_pic,
                    'content': content,
                    'comment_time': hot,
                    'user_like': user_like,
                    'num': b,

                }

                comment_hot_list.append(comment_dict)
                # 评论的格式
                datas = {
                    "word": content,
                    "color": "#ffffff",
                    "speed": random.randint(5, 35),
                    "top": random.randint(1, 100),
                    "fontSize": 16,
                    "action": "marquee",
                    "time": random.randint(1, 4),
                }
                comment_list.append(datas)
        except Exception as e:
            pass
        # 将最火评论写入json文件
        datas = json.dumps(comment_list)

        with open('./static/js/comment.json', 'w') as f:

            f.write('{"data":')
            f.write(datas)
            f.write('}')
        # 获取VIP信息
        user_id = request.session.get('user_id')
        user_vip = vip(user_id)
        is_vip = OrderInfo.objects.filter(user_id=user_id)

        context = {
            'comment_hot_list': comment_hot_list,
            'mv_name': mv_name,
            'mv_names': json.dumps(mv_name),
            'mv_id': mv_id,
            'mv_time': mv_time,
            'singer_name': singer_name,
            'singer_id': singer_id,
            'mv_240': json.dumps(mv_240),
            'mv_480': json.dumps(mv_480),
            'mv_720': json.dumps(mv_720),
            'mv_1080': json.dumps(mv_1080),
            'mv_desc': mv_desc,
            'mv_pic': mv_cover,
            'mv_play': mv_play,
            'user_vip': user_vip,
            'is_vip': is_vip,
            'user_id': user_id,
        }

        return render(request, 'mv_detail.html', context)

